#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys
import string
import MySQLdb
import ConfigParser
import logging

#read config                                                                
cf = ConfigParser.ConfigParser()
cf.read(os.path.dirname(os.path.realpath(__file__))+"/settings.conf")
host = cf.get("MySQL", "host")
user = cf.get("MySQL", "user")
passwd = cf.get("MySQL", "passwd")
dbMobileProduction = cf.get("MySQL", "dbMobileProduction")
dbStat = cf.get("MySQL", "dbStat")
dbGuid = cf.get("MySQL", "dbGuid")

def connMySQL(db):
    try:
        db=MySQLdb.connect(host=host, user=user, passwd=passwd, db=db, charset="utf8")
    except MySQLdb.Error, e:
        print e
        logging.error(e)
        return None

    #注意：上层调用的程序负责close数据库！
    #db.close()
    #或调用close函数
    return db
 
def close(db):
    db.close();

def connMySQL_mobileProduction():
    return connMySQL(dbMobileProduction)

def connMySQL_stat():
    return connMySQL(dbStat)

def connMySQL_guid():
    return connMySQL(dbGuid)

